package com.xkl.springdoc.controller.api;

import com.xkl.springdoc.common.R;
import com.xkl.springdoc.request.UserDTO;
import com.xkl.springdoc.service.IUserService;
import com.xkl.springdoc.validate.AddGroup;
import com.xkl.springdoc.validate.EditGroup;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Enumeration;

/**
 * 用户管理
 *
 * @version: 1.0.0-SNAPSHOT
 * @author: 码农小卡拉
 * @createTime: 2025-09-15 12:39
 **/
@Tag(name = "用户管理", description = "用户新增、查询、修改、删除等核心接口")
@RestController
@RequiredArgsConstructor
@RequestMapping("/api/user")
public class UserController {

    private final IUserService iUserService;

    /**
     * 根据id查询用户
     * @param id
     * @return
     */
    @GetMapping("/selectById")
    public R selectById(@RequestParam("id") Long id) {
        return iUserService.getUser(id);
    }

    /**
     * 新增用户
     * @param userDTO
     * @return
     */
    @PostMapping("/addUser")
    public R addUser(@Validated(AddGroup.class) @RequestBody UserDTO userDTO) {
        return iUserService.addUser(userDTO);
    }

    /**
     * 删除用户
     * @param id
     * @return
     */
    @DeleteMapping("/deleteById")
    public R deleteById(@RequestParam("id") Long id) {
        return iUserService.deleteUser(id);
    }

    /**
     * 更新用户
     * @param userDTO
     * @return
     */
    @PutMapping("/editUser")
    public R editUser(@Validated(EditGroup.class) @RequestBody UserDTO userDTO) {
        return iUserService.updateUser(userDTO);
    }

    /**
     * 查询所有用户
     * @return
     */
    @GetMapping("/selectAll")
    public R selectAll(HttpServletRequest request) {
        Enumeration<String> st = request.getHeaderNames();
        while (st.hasMoreElements()) {
            String key = st.nextElement();
            String value = request.getHeader(key);
            System.out.println(key + ":" + value);
        }
        return iUserService.getUsers();
    }
}
